import sun.nio.ch.ThreadPool;

import java.util.concurrent.*;

public class Demo_ThreadPool2 {
    public static void main(String[] args) {
        //1. 用来处理大量短时间工作任务的线程池，如果池中没有可用的线程将创建新的线程，如果线程空闲60秒将回收并移出缓存
        ExecutorService service1 = Executors.newCachedThreadPool();
        //2. 用来操作无界队列且固定大小的线程池
        ExecutorService service2 = Executors.newFixedThreadPool(3);
        //3. 用来操作无界队列且只有一个线程的线程池
        ExecutorService service3 = Executors.newSingleThreadExecutor();
        //4. 创建一个单线程执行器，可以在给定的时间后执行或定期执行
        ExecutorService service4 = Executors.newSingleThreadScheduledExecutor();
        //5. 创建一个给定大小的线程池，可以在给定时间后执行或者定期执行
        ExecutorService service5 = Executors.newScheduledThreadPool(3);
        //6. 创建一个指定大小的（不传参数，为当前机器核心数）线程池，并行处理任务，不保证顺序
        ExecutorService service6 = Executors.newWorkStealingPool();
        //7. 自定义线程池
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
                3,
                10,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(10)
        );

    }
    public static void main5(String[] args) {
        ExecutorService service = Executors.newWorkStealingPool();
        for(int i = 0; i < 100; i++){
            int id = i;
            service.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "执行任务：" + id);
                }
            });
        }
    }
    public static void main4(String[] args) {
        ExecutorService service = Executors.newScheduledThreadPool(5);
        for(int i = 0; i < 100; i++){
            int id = i;
            service.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "执行任务：" + id);
                }
            });
        }
    }
    public static void main3(String[] args) {
        ExecutorService service = Executors.newSingleThreadExecutor();
        for(int i = 0; i < 100; i++){
            int id = i;
            service.submit(new Runnable() {
                @Override
                public void run() {
                    Thread current = Thread.currentThread();
                    System.out.println(current.getName() + "执行任务：" + id);
                }
            });
        }
    }
    public static void main2(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(5);
        for(int i = 0; i < 100; i++){
            int id = i;
            service.submit(new Runnable() {
                @Override
                public void run() {
                    Thread current = Thread.currentThread();
                    System.out.println(current.getName() + "执行任务：" + id);
                }
            });
        }
    }
    public static void main1(String[] args) {
        ExecutorService service = Executors.newCachedThreadPool();
        for(int i = 0; i < 100; i++){
            int id = i;
            service.submit(new Runnable() {
                @Override
                public void run() {
                    Thread current = Thread.currentThread();
                    System.out.println(current.getName() + "执行任务" + id);
                }
            });
        }
    }
}
